'use strict';
var xxx;
(function() {
	var position = (function($) {
		var
		common = {
			form : $('#PositionForm'),
			submit : function() {
				if (this.fieldTest()) {

					xxx = this.form;

					$.post('/from/update-position', this.form.serialize(), function(data) {
						if (data == "success") {
							layer.msg('保存成功！');
						} else {
							layer.msg(data + ',保存失败。', {icon : 5});
						}
					});
				}
			},
			fieldTest : function() {
				return true;
			},
		},
		position = {
			batchBtn : $("#batchBtn"),
			updateBtn : $(".updateBtn"),
			init : function() {
				this.update();
			},
			update : function() {
				var _this = this;

				this.updateBtn.click(function() {

						_this.editing(this);
						$(this).html("");
						$(_this.batchBtn).html("完成");
				});

				this.batchBtn.click(function() {

					if ($(this).html() != '完成'){
					
						$("input[name='pk_id[]']").each(function() {
							if (/待修改/.test($(this).parent().html())) {
								_this.editing(this);
								$(this).removeAttr("disabled");
							}
						});
						$(_this.updateBtn).html("");
						$(this).html("完成");
					} else {

						common.submit();
						_this.close();
						$(this).html("批量修改");
					}
				});
			},
			close : function() {
				$("input[type=text]").each(function() {
					$(this).parent().html(this.value);
				});

				$("input[type=hidden]").each(function() {
					$(this).attr("disabled", "true");
				});

				$(".updateBtn").html('<span class="glyphicon glyphicon-pencil"></span>');
			},
			editing : function(thisBtn) {
				var names = new Array("case_a", "case_b", "xna_x", "xna_y", "xna_z", "xna_density", "xna_volumn"),
						tds = $(thisBtn).parent().siblings("td");

				for (var i = 3; i < 10; i ++) {
					var string = '<input name="' + names[i-3] + '[]" type="text" value="' + $(tds[i]).text() + '" />';
					$(tds[i]).html(string);
				}

				$(tds[10]).find("input").removeAttr("disabled");
			}
		},
		init = function() {
			position.init();
		};
		return {
			init : init
		};
	})(jQuery);

	position.init();
})();